<?php

namespace app\admin\controller;
class ArticleCate extends base
{


    public function index()
    {
        $list = $this->model->catetree();
        $this->assign('list', $list);
        $this->assign('num', $this->model->count());
        return $this->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        $getall = $this->model->catetree();
        $this->assign('getall', $getall);
        return $this->fetch('edit');
    }

    /**
     * 修改
     */
    public function edit($id)
    {
        if (intval($id) < 1) {
            $this->error('数据错误');
        }
        $data = $this->model->get($id);

        $getall = $this->model->outpid($id);

        $this->assign('data', $data);
        $this->assign('getall', $getall);
        return $this->fetch();
    }

    public function save()
    {
        if (!$this->request->isPost()) {
            $this->error('请求失败');
        }

        $data = $this->request->post();

        $result = $this->validate($data, 'ArticleCate');
        if (true !== $result) {
            // 验证失败 输出错误信息
            $this->error($result);
        }

        if (!empty($data['id'])) {
            return $this->update($data);
        }

        if ($this->model->allowField(true)->save($data)) {
            $this->success('添加成功');
        } else {
            $this->error('添加失败');
        }

        $getall = $this->model->catetree();
        $this->assign('getall', $getall);
        return $this->fetch();

    }



}
